package com.robot.controller;

import com.robot.dao.InterfaceMapper;
import com.robot.model.Protocol;
import com.robot.model.User;
import com.robot.service.DubboService;
import com.robot.service.InterfaceService;
import com.robot.util.SessionUtil;
import com.robot.vo.ResponseMessage;
import com.robot.vo.TablePage;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/10/13
 * Time: 9:41
 * PROJECT：robot
 * PACKAGE: com.robot
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/interface")
@Slf4j
public class InterfaceController {
    @Autowired
    InterfaceService interfaceService;
    @Autowired
    InterfaceMapper interfaceMapper;
    @Autowired
    DubboService dubboService;

    @RequestMapping("/interfaceList")
    public @ResponseBody
    ResponseMessage list(Protocol protocol) {
        User userDo = (User) SessionUtil.getSessionUser();
        Integer projectId = userDo.getLastProjectId();
        protocol.setProjectId(projectId);
        return interfaceService.interfaceList(protocol);
    }

    @RequestMapping("/page")
    public ModelAndView page() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/interface/interfaceList");
        /**这里可以改成从数据库取值，或者从其他的配置文件里面取值**/
        List<String> interfaceTypeList = Lists.newArrayList();
        interfaceTypeList.add("dubbo");
        interfaceTypeList.add("http");
        modelAndView.addObject("interfaceTypeList", interfaceTypeList);
        return modelAndView;
    }

    @RequestMapping("/modifyInterfacePage")
    public ModelAndView modifyInterfacePage(Integer id) {
        Protocol protocol = interfaceMapper.getProtocolById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("protocol", protocol);
        if (protocol.getInterfaceType().equalsIgnoreCase("dubbo")) {
            ResponseMessage responseMessage = dubboService.interfaceList(protocol.getHeader());
            if (responseMessage.isSuccess()) {
                modelAndView.addObject("interfaceList", responseMessage.getData());
            }
            modelAndView.setViewName("/interface/dubboForm");
        } else if (protocol.getInterfaceType().equalsIgnoreCase("http")) {
            modelAndView.setViewName("/interface/httpForm");
        }
        return modelAndView;
    }

    @RequestMapping("/addInterfacePage")
    public ModelAndView addInterfacePage(String interfaceType) {
        ModelAndView modelAndView = new ModelAndView();
        /**通过interfaceType判断需要跳转到哪个页面**/
        if (interfaceType.equalsIgnoreCase("dubbo")) {
            Protocol protocol = new Protocol();
            protocol.setHeader("47.105.55.243:2181");
            modelAndView.addObject("protocol", protocol);
            ResponseMessage responseMessage = dubboService.interfaceList("47.105.55.243:2181");
            if (responseMessage.isSuccess()) {
                modelAndView.addObject("interfaceList", responseMessage.getData());
            }
            modelAndView.setViewName("/interface/dubboForm");
        } else if (interfaceType.equalsIgnoreCase("http")) {
            modelAndView.setViewName("/interface/httpForm");
        }
        return modelAndView;
    }

    @RequestMapping("/deleteInterface")
    public @ResponseBody ResponseMessage delete(Integer interfaceId){
        try {
            interfaceService.delete(interfaceId);
        }catch (Exception e){
            log.error("删除异常",e);
            return ResponseMessage.errorResponse("删除接口异常,请联系管理员");
        }
        return ResponseMessage.successResponse();
    }
    @RequestMapping("/saveInterface")
    public ModelAndView saveInterface(@Valid Protocol protocol, BindingResult bindingResult) {
        ModelAndView modelAndView = new ModelAndView();
        if (bindingResult.hasErrors()) {
            if (protocol.getInterfaceType().equalsIgnoreCase("dubbo")) {
                ResponseMessage responseMessage = dubboService.interfaceList(protocol.getHeader());
                if (responseMessage.isSuccess()) {
                    modelAndView.addObject("interfaceList", responseMessage.getData());
                }
                modelAndView.setViewName("/interface/dubboForm");
            } else {
                modelAndView.setViewName("/interface/httpForm");
            }
            modelAndView.addObject("protocol", protocol);
            String message = bindingResult.getFieldError().getDefaultMessage();
            modelAndView.addObject("errorMsg", message);
            return modelAndView;
        }
        User userDo = (User) SessionUtil.getSessionUser();
        Integer projectId = userDo.getLastProjectId();
        protocol.setProjectId(projectId);
        Integer id = protocol.getId();
        ResponseMessage responseMessage=null;
        /**如果主键id等于null的时候，认为是新增，否则认为是修改**/
        if(null ==id){
            responseMessage = interfaceService.saveInterface(protocol);
        }else {
            responseMessage =interfaceService.modifyInterface(protocol);
        }
        if (responseMessage.isSuccess()) {
            modelAndView.setViewName("/interface/page");
        }
        return modelAndView;
    }
}
